window.onload = function () {
    findByItem(1);
}

/**
 * 动态条件分页查询
 * @param pageNO 页码
 */
function findByItem(pageNO) {
    axios.get("/product/findByItem", {
        params: {
            pageNO,
            name: $("nameTxt").value,
            startDate: $("startTxt").value,
            endDate: $("endTxt").value
        }
    }).then(resp => {
        var info = resp.data;
        //表格
        var str = "";
        info.list.forEach(n => {
            str += `<tr><td>${n.name}</td><td>${n.createDate}</td><td>${n.price}</td>
                <td><a href="/product/update.html?id=${n.id}">修改</a><a onclick="del(${n.id})">删除</a></td></tr>`;
        });
        $("data").innerHTML = str;

        //页码
        var pageStr = "";
        for (var i = 1; i <= info.totalPage; i++) {
            pageStr += `<a href="javascript:findByItem(${i})" style="margin-left: 10px">${i}</a>`;
        }
        $("cutDiv").innerHTML = pageStr;
    });
}

function del(id) {
    axios.get("/product/del", {
        params: {
            id
        }
    }).then(resp => {
        var info = resp.data;
        if (info == "ok") {
            findByItem(1)
            alert("删除成功")
        }
    })
}